﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace RTS
{
	class GameGraph
	{
		private Camera camera;
		private Background background = new Background();
		private GameMain game;

		public GameGraph(GameMain game)
		{
			this.game = game;
			camera = new Camera();

			assingCameras();
		}

		private void preloadGraphics(ContentManager Content)
		{
			List<PosicionableObject> objectsToPreload = new List<PosicionableObject>();

			objectsToPreload.Add(new LanceMan(game));
			objectsToPreload.Add(new CrossBowMan(game));
			objectsToPreload.Add(new MagicMan(game));
			objectsToPreload.Add(new MageTower(game));

			foreach (PosicionableObject posicionable in objectsToPreload)
			{
				posicionable.LoadContent(Content);
			}
		}

		private void assingCameras()
		{
			List<PosicionableObject> posicionables = game.getPosicionableContent();

			background.assignCamera(camera);

			for (int i = 0; i < posicionables.Count; i++)
			{
				posicionables[i].assignCamera(camera);
			}
		}

		public void loadContent(ContentManager Content)
		{
			preloadGraphics(Content);


			List<PosicionableObject> posicionables = game.getPosicionableContent();
			background.loadContent(Content);

			for (int i = 0; i < posicionables.Count; i++)
			{
				posicionables[i].LoadContent(Content);
			}

		}

		private void sortPosicionables(List<PosicionableObject> posicionable)
		{
			posicionable.Sort(
				delegate(PosicionableObject objA, PosicionableObject objB)
				{
					Position2D posA = objA.getPosition();
					Position2D posB = objB.getPosition();

					return posA.Y.CompareTo(posB.Y);
				});
		}

		public void draw(SpriteBatch spriteBatch)
		{
			List<PosicionableObject> posicionables = game.getPosicionableContent();
			sortPosicionables(posicionables);

			background.draw(spriteBatch);

			for (int i = 0; i < posicionables.Count; i++)
			{
				posicionables[i].Draw(spriteBatch);
			}
			
		}

		public void makeClick(MouseState mouse)
		{
			List<PosicionableObject> posicionables = game.getPosicionableContent();
			List<PosicionableObject> clickedObjects = getClickedObjects(mouse);

			for (int i = 0; i < posicionables.Count; i++)
			{
				posicionables[i].excecuteClickAction(clickedObjects, mouse);
			}

		}

		private List<PosicionableObject> getClickedObjects(MouseState mouse)
		{
			List<PosicionableObject> posicionables = game.getPosicionableContent();
			List<PosicionableObject> clickedObjects = new List<PosicionableObject>();

			for (int i = 0; i < posicionables.Count; i++)
			{
				if (posicionables[i].isClicked(mouse.X, mouse.Y)) 
					clickedObjects.Add(posicionables[i]);
			}

			sortPosicionables(clickedObjects);	// For getting the element that is above first
			clickedObjects.Reverse();
			return clickedObjects;
		}

		public void update(GraphicsDeviceManager graphics)
		{
			camera.update(graphics);
		}

	}
}
